package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateUserOrderDetail
//@description: 创建UserOrderDetail记录
//@param: userOrderDetail model.UserOrderDetail
//@return: err error

func CreateUserOrderDetail(userOrderDetail model.UserOrderDetail) (err error) {
	err = global.GVA_DB.Create(&userOrderDetail).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserOrderDetail
//@description: 删除UserOrderDetail记录
//@param: userOrderDetail model.UserOrderDetail
//@return: err error

func DeleteUserOrderDetail(userOrderDetail model.UserOrderDetail) (err error) {
	err = global.GVA_DB.Delete(&userOrderDetail).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserOrderDetailByIds
//@description: 批量删除UserOrderDetail记录
//@param: ids request.IdsReq
//@return: err error

func DeleteUserOrderDetailByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserOrderDetail{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateUserOrderDetail
//@description: 更新UserOrderDetail记录
//@param: userOrderDetail *model.UserOrderDetail
//@return: err error

func UpdateUserOrderDetail(userOrderDetail model.UserOrderDetail) (err error) {
	err = global.GVA_DB.Save(&userOrderDetail).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserOrderDetail
//@description: 根据id获取UserOrderDetail记录
//@param: id uint
//@return: err error, userOrderDetail model.UserOrderDetail

func GetUserOrderDetail(id uint) (err error, userOrderDetail model.UserOrderDetail) {
	err = global.GVA_DB.Where("id = ?", id).Preload(`BuyUsers`).Preload(`SellerUser`).Preload(`UserOrder`).Preload(`CWorkInfo`).Preload(`CProductInfo`).First(&userOrderDetail).Error
	return
}
func DeleteUserOrderDetailByOrderId(OrderId int) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserOrderDetail{},"order_id = ?",OrderId).Error
	return err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserOrderDetailInfoList
//@description: 分页获取UserOrderDetail记录
//@param: info request.UserOrderDetailSearch
//@return: err error, list interface{}, total int64

func GetUserOrderDetailInfoList(info request.UserOrderDetailSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.UserOrderDetail{})
    var userOrderDetails []model.UserOrderDetail
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.BuyId != 0 {
        db = db.Where("`buy_id` = ?",info.BuyId)
    }
    if info.CProduct != 0 {
        db = db.Where("`c_product` = ?",info.CProduct)
    }
    if info.CWork != 0 {
        db = db.Where("`c_work` = ?",info.CWork)
    }
    if info.Name != "" {
        db = db.Where("`name` LIKE ?","%"+ info.Name+"%")
    }
    if info.OrderId != 0 {
        db = db.Where("`order_id` = ?",info.OrderId)
    }
    if info.SellerId != 0 {
        db = db.Where("`seller_id` = ?",info.SellerId)
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Preload(`BuyUsers`).Preload(`SellerUser`).Preload(`UserOrder`).Preload(`CWorkInfo`).Preload(`CProductInfo`).Find(&userOrderDetails).Error
	return err, userOrderDetails, total
}


func GetUserOrderDetailInfoLists(info request.UserOrderDetailSearch) (err error, list interface{}, total int64) {
	var infos  request.UserOrderSearch
	flags := true
	infos.IsPublic = &flags
	infos.Page = 1
	infos.PageSize = 10000
	_,data,_ := GetUserOrderInfoList(infos)
	datas := data.([]model.UserOrder)
	idList := []uint{}
	for _,v := range datas{
		idList = append(idList,v.ID)
	}
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.UserOrderDetail{})
	var userOrderDetails []model.UserOrderDetail
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.BuyId != 0 {
		db = db.Where("`buy_id` = ?",info.BuyId)
	}
	if info.CProduct != 0 {
		db = db.Where("`c_product` = ?",info.CProduct)
	}
	if info.CWork != 0 {
		db = db.Where("`c_work` = ?",info.CWork)
	}
	if info.Name != "" {
		db = db.Where("`name` LIKE ?","%"+ info.Name+"%")
	}
	if info.OrderId != 0 {
		db = db.Where("`order_id` = ?",info.OrderId)
	}
	if info.SellerId != 0 {
		db = db.Where("`seller_id` = ?",info.SellerId)
	}
	db = db.Where("`order_id` in ?",idList)
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Preload(`UserOrder.BuyUsers`).Preload(`UserOrder.SellerUser`).Preload(`UserOrder`,`is_public = 1`).Preload(`CWorkInfo`).Preload(`CProductInfo`).Order(`order_id desc`).Find(&userOrderDetails).Error
	return err, userOrderDetails, total
}